<?php
class ScoresController extends AppController {

	var $name = 'Scores';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Score->recursive = 0;
			$this->set('scores', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function view($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid score', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('score', $this->Score->read(null, $id));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		if(empty($this->data)){
				$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
				$this->redirect(array('controller'=>'pages','action' => 'index'));
			}
		if (!empty($this->data) && $this->Auth->user('id') ) {
			$data = $this->data;
			$isRated = $this->Score->find('count',array('conditions'=>array('product_id'=>$data['Score']['product_id'], 'user_id'=>$data['Score']['user_id'])));
			if($isRated == 0){
				$this->Score->create();
				if ($this->Score->save($this->data)) {
					$this->Score->recursive = -1;
					$this->saveNewScore($this->data['Score']['product_id'],$this->data['Score']['score']);
	                if($this->RequestHandler->isAjax()){
	                	$product_id = $this->data['Score']['product_id'];
	                	$this->loadModel('Product');
		                $this->autoRender = false;
		                $this->Product->contain();
		                $score = $this->Product->find('first',array('conditions'=>array('Product.id'=>$product_id),'fields'=>'Product.score'));
		                return(json_encode($score));
					}else {
						$this->Session->setFlash(__('The score has been saved', true));
	                	//$this->redirect(array('action' => 'index'));
					}
				} else {
					$this->Session->setFlash(__('The score could not be saved. Please, try again.', true));
				}
			} 			
		}
		$products = $this->Score->Product->find('list');
		$users = $this->Score->User->find('list');
		$this->set(compact('products', 'users'));
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid score', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->Score->save($this->data)) {
					$this->Session->setFlash(__('The score has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The score could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Score->read(null, $id);
			}
			$products = $this->Score->Product->find('list');
			$users = $this->Score->User->find('list');
			$this->set(compact('products', 'users'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for score', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Score->delete($id)) {
				$this->Session->setFlash(__('Score deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Score was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
    
    function saveNewScore($product_id = null, $newScore = null){
    	$this->loadModel('Product');
    	$this->Product->contain();
    	$product = $this->Product->find('first',array('conditions'=>array('id'=>$product_id),'fields'=>array('score')));
    	$oldScore = $product['Product']['score'];
    	$scoreCount = $this->Score->find('count',array('conditions'=>array('product_id'=>$product_id)));
   		if($scoreCount==1){
   			$oldScore = 0;
   		}
    	$newScore = (($oldScore * ($scoreCount - 1)) + $newScore) / $scoreCount;
   		$this->Product->id = $product_id;
   		$this->Product->saveField('score',$newScore);
    }
}
